package com.onlineshopping.controller;

import com.onlineshopping.entity.Item;
import com.onlineshopping.mapper.ItemMapper;
import com.onlineshopping.vo.SearchQuery;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author laisir
 * @date 2024/9/23
 */
@RestController
@RequestMapping("/item")
public class ItemController {

    @Resource
    private ItemMapper itemMapper;
    @GetMapping("/items")
    public List<Item> getAll(){
        return itemMapper.getAll();
    }

    @PostMapping("/search")
    public List<Item> search(@RequestBody SearchQuery searchQuery){
        String search = searchQuery.getSearch();
        //System.out.println(search);
        return itemMapper.search(search);
    }


    @DeleteMapping("/deleteItem/{id}")
    public String deleteItem(@PathVariable Long id){
        // 处理删除逻辑
        //System.out.println("Deleting item with ID: " + id);
        itemMapper.deleteItem(id);
        return "Item deleted successfully";
    }

    @PutMapping("/updateItem/{id}")
    public String updateItem(@PathVariable Long id, @RequestBody Item item) {
        //System.out.println("Updating item with ID: " + id);
        //System.out.println(item);
        // 更新商品信息
        itemMapper.updateItem(item);
        return "Item updated successfully";
    }

    @PostMapping("/add")
    public String addProduct(@RequestBody Item item) {
        // 处理添加商品逻辑
        itemMapper.addProduct(item);
        return "Product added successfully";
    }
}
